﻿using System;
using élénkPróbája.native;
using élénkPróbája.native.rule;

namespace élénkPróbája.validators
{
    public class PropertyValidatorContext
    {
        private bool propertyValueSet;
        private readonly Lazy<object> propertyValueContainer;

        public ValidationContext ParentContext { get; private set; }
        public PropRule Rule { get; private set; }
        public string PropertyName { get; private set; }

        public string PropertyDescription => Rule.GetDisplayName();

        public object Instance => ParentContext.InstanceToValidate;

        public MessageFormatter MessageFormatter { get; } = new MessageFormatter();

        //Lazily load the property value
        //to allow the delegating validator to cancel validation before value is obtained
        public object PropertyValue => propertyValueContainer.Value;

        public PropertyValidatorContext(ValidationContext parentContext, PropRule rule, string propertyName)
        {
            ParentContext = parentContext;
            Rule = rule;
            PropertyName = propertyName;
            propertyValueContainer = new Lazy<object>(() => rule.PropFunc(parentContext.InstanceToValidate));
        }


        public PropertyValidatorContext(ValidationContext parentContext, PropRule rule, string propertyName, object propertyValue)
        {
            ParentContext = parentContext;
            Rule = rule;
            PropertyName = propertyName;
            propertyValueContainer = new Lazy<object>(() => propertyValue);
        }
    }
}